﻿using System;
using System.Collections;
using System.Data;
using viBlog.Providers;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.Pages;

namespace viBlog.Controllers
{
    /// <summary>
    /// BlogController is the blog main settings controller
    /// it manages the blog name, theme etc.
    /// </summary>
    public class BlogController
    {
        private const string KEY_BLOG_SETTINGS = "blogSettings";
        
        /// <summary>
        /// The blog name
        /// </summary>
        public static string BlogName
        {
            get { return (string)Settings["BlogName"]; }
            set { AddSetting("BlogName", value); }
        }

        /// <summary>
        /// A line about the blog
        /// </summary>
        public static string BlogTagLine
        {
            get { return (string)Settings["BlogTagLine"]; }
            set { AddSetting("BlogTagLine", value); }
        }
             
        /// <summary>
        /// The url separator for the viBlog plugin
        /// </summary>
        public static string BlogUrlSeparator
        {
            get { return PluginController.GetInstalledPlugin("viBlog").UrlSeparator; }
        }

        /// <summary>
        /// The current version of the viBlog plugin
        /// </summary>
        public static string BlogVersion
        {
            get { return PluginController.GetInstalledPlugin("viBlog").Version; }
        }

        /// <summary>
        /// The page name of the blog used internally
        /// </summary>
        public static string BlogPageName
        {
            get { return "viBlogPage"; }
        }
         
        /// <summary>
        /// The blog page id used internally
        /// </summary>
        public static int BlogPageId
        {   
            get 
            {
                if(Settings["BlogPageId"] != null)
                    return Convert.ToInt32(Settings["BlogPageId"]);
                PageInfo page = PageController.GetPage("BlogPageId");
                if (page == null)
                {
                    Plugin plugin = PluginController.GetInstalledPlugin("viBlog");
                    int pageid = PageController.AddPage(new PageInfo(BlogPageName, "Blog", plugin.PluginId));
                    AddSetting("BlogPageId", pageid.ToString());
                    return pageid;
                }
                else
                {
                    AddSetting("BlogPageId", page.PageId.ToString());
                    return page.PageId;
                }
            }
            set { AddSetting("BlogPageId", value.ToString()); }
        }
                
        /// <summary>
        /// The count of posts to show on the page
        /// </summary>
        public static int PostsCountPerPage
        {
            get { return Convert.ToInt32(Settings["PostsCountPerPage"]); }
            set { AddSetting("PostsCountPerPage", value.ToString()); }
        }

        /// <summary>
        /// The count of posts to syndicate
        /// </summary>
        public static int PostsCountToSyndicate
        {
            get { return Convert.ToInt32(Settings["PostsCountToSyndicate"]); }
            set { AddSetting("PostsCountToSyndicate", value.ToString()); }
        }

        /// <summary>
        /// The comment moderation status
        /// </summary>
        public static bool ModerateComments
        {
            get { return GetValue(); }
            set { AddSetting("ModerateComments", value.ToString()); }
        }

        private static bool GetValue()
        {
            if (Settings["ModerateComments"].ToString().ToLower() == "true")
                return true;
            else
                return false;
        }

        /// <summary>
        /// Gets the blog setting
        /// </summary>
        /// <param name="key">The settings key to get</param>
        /// <returns>returns the setting value</returns>
        public static string GetSetting(string key)
        {
            if (Settings.Contains(key))
                return (string)Settings[key];
            else
                return string.Empty;
        }

        private static Hashtable _Settings;
        private static Hashtable Settings
        {
            get
            {
                _Settings = (Hashtable)DataCache.GetCache(KEY_BLOG_SETTINGS);
                if (_Settings == null)
                {
                    _Settings = new Hashtable();
                    IDataReader dr = viBlogDataProvider.Provider.GetBlogSettings();
                    while (dr.Read())
                    {
                        if (dr["SettingName"] != Convert.DBNull)
                        {
                            if (dr["SettingValue"] != Convert.DBNull)
                                _Settings[(string)dr["SettingName"]] = (string)dr["SettingValue"];
                            else
                                _Settings[(string)dr["SettingName"]] = "";
                        }
                    }
                    dr.Close();
                    DataCache.SetCache(KEY_BLOG_SETTINGS, _Settings);
                }
                return _Settings;
            }
        }

        #region DATA
        /// <summary>
        /// Adds a new blog setting Or updates it if it already exists
        /// </summary>
        /// <param name="key">The key to setting</param>
        /// <param name="value">The value of the setting</param>
        public static void AddSetting(string key, string value)
        {
            if (Settings.ContainsKey(key))
                viBlogDataProvider.Provider.UpdateBlogSetting(key, value);
            else
                viBlogDataProvider.Provider.AddBlogSetting(key, value);
            DataCache.DeleteCacheMultiple("blog");
        }
        /// <summary>
        /// Deletes the site setting
        /// </summary>
        /// <param name="key">The key to delete</param>
        public static void DeleteSetting(string key)
        {
            viBlogDataProvider.Provider.DeleteBlogSetting(key);
            DataCache.DeleteCacheMultiple("blog");
        }
        #endregion
    }
}
